package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.result.PageResult;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.anno.RequiredPermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@Service
public class PermissionServiceImpl implements IPermissionService {

    @Autowired
    private PermissionMapper permissionMapper;
    @Autowired
    private ApplicationContext ctx;

    @Override
    public void delete(Long id) {
       permissionMapper.deleteByPrimaryKey(id);

    }

    @Override
    public void insert(Permission permission,Long[] ids) {
       permissionMapper.insert(permission);
    }

    @Override
    public Permission selectByPrimaryKey(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> selectAll() {
        return permissionMapper.selectAll();
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(), qo.getPageSize());
        List<Permission> permissions = permissionMapper.queryForList(qo);
        return new PageInfo(permissions);
    }


    public void reload(){
        //需求:把每个控制器中贴了权限注解的方法转成权限数据,并放到数据库中
        //先拿到数据库中现有的权限表达式
        List<String> expressions = permissionMapper.queryForPermissionExpression();
        //1.先拿到所有的贴有Controller注解的控制器
        Map<String, Object> map = ctx.getBeansWithAnnotation(Controller.class);
        //2.遍历获取每个控制器中的方法
        Collection<Object> collections = map.values();
        //3.遍历每一个方法
        for (Object collection : collections) {
            //因为现在我们使用了shiro的授权器的扫描器,它会产生一个cglib代理类,该代理类是继承与实际类,所以我们需要调用
            //父类的字节码对象
            Class<?> clazz = collection.getClass().getSuperclass();
            Method[] methods = clazz.getDeclaredMethods();

            for (Method method : methods) {
                //4.判断并找到贴了我们需要的权限的方法
                RequiresPermissions annotation = method.getAnnotation(RequiresPermissions.class);
                if (annotation != null){
                    //5.从注解中获取信息,并封装到权限对象中
                    String name = annotation.value()[1];
                    String expression = annotation.value()[0];
                    //插入之前先判断数据库中是否有该对象了(通过表达式判断)
                    //6.把该对象插入到数据库中
                    if (!expressions.contains(expression)){
                        Permission permission = new Permission(name, expression);
                        permissionMapper.insert(permission);
                    }
                }
            }
        }
    }
}
